package com.gree.flowable.app.controller;

import com.gree.flowable.app.pojo.ex.FormInfoExModelArr;
import com.gree.flowable.app.pojo.form.FormInfo;
import com.gree.flowable.app.pojo.form.FormModel;
import com.gree.flowable.app.pojo.model.PagerModel;
import com.gree.flowable.app.pojo.model.Query;
import com.gree.flowable.app.pojo.to.QueryFormInfoTO;
import com.gree.flowable.app.pojo.vo.ReturnVO;
import com.gree.flowable.app.services.IFormInfoService;
import com.gree.flowable.app.tools.constants.ReturnCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：180849
 * @date ：Created in 2021/3/30 16:52
 * @description：表单信息
 * @modified By：
 * @version: 1.0
 */
@RestController

@Api(tags = "表单信息Api")
@RequestMapping("/rest/formInfo")
public class IFormInfoApi extends FlowableBaseApi {

    @Autowired
    private IFormInfoService iFormInfoService;

    @RequestMapping(value = "/addForm",method = RequestMethod.POST)
    @ApiOperation("添加表单")
    @ApiImplicitParam(name = "FormInfo", value = "表单信息", dataType = "FormInfo")
    @ResponseBody
    public ReturnVO addFormInfoForString(@RequestBody FormInfo FormInfo){

        Boolean flag = iFormInfoService.addFormInfoForString(FormInfo);
        if (flag){
            return new ReturnVO(ReturnCode.SUCCESS,"插入成功");
        } else {
            return new ReturnVO(ReturnCode.FAIL,"插入失败");
        }
    }

    @GetMapping("/queryForm/{formId}")
    @ResponseBody
    @ApiOperation("查询表单信息")
    @ApiImplicitParam(name = "formId",value = "表单ID",dataType = "String")
    public ReturnVO queryFormById(@PathVariable String formId){
        FormInfo formInfoVO = iFormInfoService.queryFormById(formId);
        String formInfo = formInfoVO == null ? "":formInfoVO.getFormInfo();
        return new ReturnVO(ReturnCode.SUCCESS,"查询成功",formInfo);
    }

    @GetMapping("/deleteForm/{formId}")
    @ResponseBody
    @ApiOperation("删除表单模型")
    @ApiImplicitParam(name = "formId",value = "表单ID",dataType = "String")
    public ReturnVO deleteFormInfo(@PathVariable String formId){
        Integer deleteFormTotal = iFormInfoService.deleteFormInfo(formId);
        if (deleteFormTotal == 1){
            return new ReturnVO(ReturnCode.SUCCESS,"删除成功");
        } else {
            return new ReturnVO(ReturnCode.FAIL,"删除失败");
        }
    }

    @RequestMapping(value = "/queryFormList",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("分页模糊查询主表单模型")
    @ApiImplicitParam(name = "params",value = "表单id与表单名称",dataType = "QueryFormInfoTO")
    public PagerModel<FormInfo> queryFormList(@RequestBody QueryFormInfoTO params){
        Query query = params.getQuery();
        return iFormInfoService.queryFormList(params,query);
    }

    @PostMapping("/model")
    @ApiOperation("更新表单模型")
    @ApiImplicitParam(name = "params",value = "表单模型组件",dataType = "FormInfoExModelArr")
    public ReturnVO saveModel(@RequestBody FormInfoExModelArr params){
        return iFormInfoService.saveModel(params);
    }

    @GetMapping("/table/{formId}")
    @ResponseBody
    @ApiOperation("查询明细表格信息")
    @ApiImplicitParam(name = "formId",value = "表单ID",dataType = "String")
    public ReturnVO queryTableById(@PathVariable String formId){
        List<FormModel> formModels = iFormInfoService.queryTableById(formId);

        return new ReturnVO(ReturnCode.SUCCESS,"查询成功", formModels);
    }
}
